home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 Extra Demos / Brian's Demos / Extended Sprite Test / About.c next >
Encoding:
Text File  |  1998-12-31  |  8.0 KB  |  334 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    About.c
  3. //
  4. //    By:        Tony Myles
  5. //
  6. //    Copyright: © 1993-94 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9. #include <Events.h>
  10.  
  11. #include <SWIncludes.h>            // Automatically include all SpriteWorld.h files
  12.  
  13. #ifndef __DIALOGUTILS__
  14. #include <SWDialogUtils.h>
  15. #endif
  16.  
  17. #ifndef __SPRITETEST__
  18. #include "Extended SpriteTest.h"
  19. #endif
  20.  
  21. #ifndef __APPLICATION__
  22. #include "Application.h"
  23. #endif
  24.  
  25. #ifndef __ABOUT__
  26. #include "About.h"
  27. #endif
  28.  
  29.  
  30. extern SpriteTestPtr gSpriteTestP;
  31.  
  32.  
  33. ///--------------------------------------------------------------------------------------
  34. // DisplayAboutBox
  35. ///--------------------------------------------------------------------------------------
  36.  
  37. void DisplayAboutBox(void)
  38. {
  39.     OSErr                 err;
  40.     GrafPtr             savePort;
  41.     DialogPtr             aboutDialogP;
  42.     SpriteWorldPtr         spriteWorldP = NULL;
  43.     SpriteLayerPtr         spriteLayerP = NULL;
  44.     SpritePtr             earthSpriteP = NULL; 
  45.     SpritePtr            shadowSpriteP = NULL;
  46.     Rect                 itemRect, moveBoundsRect;
  47.     short                 itemHit, itemType;
  48.     Handle                itemHandle;
  49.     ModalFilterUPP        spriteWorldDialogFilter;
  50.     UserItemUPP            procForVersUserItem;
  51.  
  52.     procForVersUserItem = NewUserItemProc(DrawVersionProc);
  53.  
  54.     aboutDialogP = GetNewDialog(kAboutDialogID, NULL, (WindowPtr)-1L);
  55.     
  56.     GetDialogItem(aboutDialogP, 6, &itemType, &itemHandle, &itemRect);
  57.     SetDialogItem(aboutDialogP, 6, itemType, (Handle)procForVersUserItem, &itemRect);
  58.     
  59.     if (aboutDialogP != NULL)
  60.     {
  61.         GetPort(&savePort);
  62.         SetPort(aboutDialogP);
  63.  
  64.             // create the sprite world
  65.         err = SWCreateSpriteWorldFromWindow(&spriteWorldP, (CWindowPtr)aboutDialogP, NULL, NULL, 0);
  66.     
  67.         if (err == noErr)
  68.         {    
  69.                 // create the sprite layer
  70.             err = SWCreateSpriteLayer(&spriteLayerP);
  71.         }
  72.  
  73.         if (err == noErr)
  74.         {
  75.             err = SWCreateSpriteFromCicnResource(
  76.                 spriteWorldP,
  77.                 &earthSpriteP, 
  78.                 NULL, 
  79.                 kEarthCIconID, 
  80.                 1, 
  81.                 kRegionMask);
  82.         }
  83.  
  84.         if (err == noErr)
  85.         {
  86.             err = SWCreateSpriteFromCicnResource(
  87.                 spriteWorldP, 
  88.                 &shadowSpriteP, 
  89.                 NULL, 
  90.                 kShadowCIconID,
  91.                 kNumberOfShadowFrames, 
  92.                 kRegionMask);
  93.         }
  94.  
  95.         if (err == noErr)
  96.         {
  97.             shadowSpriteP->userData = (long)earthSpriteP;
  98.  
  99.             SWAddSprite(spriteLayerP, shadowSpriteP);
  100.             SWAddSprite(spriteLayerP, earthSpriteP);
  101.             SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  102.  
  103.             GetDItemRect(aboutDialogP, 4, &itemRect);
  104.             moveBoundsRect = aboutDialogP->portRect;
  105.             moveBoundsRect.bottom = itemRect.top + ((itemRect.bottom-itemRect.top)/2);
  106.  
  107.                 // set the sprite’s movement characteristics
  108.             SWSetSpriteMoveBounds(earthSpriteP, &moveBoundsRect);
  109.             SWSetSpriteMoveDelta(earthSpriteP, 0, kInitialSpeed);
  110.             SWSetSpriteMoveProc(earthSpriteP, EarthMoveProc);
  111.             SWSetSpriteMoveTime(earthSpriteP, 55);
  112.             SWSetSpriteMoveTime(shadowSpriteP, -1);        // never move
  113.             SWSetSpriteFrameTime(shadowSpriteP, 0);
  114.             SWSetSpriteFrameRange(shadowSpriteP, 0, 4);
  115.             SWSetSpriteFrameProc(shadowSpriteP, ShadowFrameProc);
  116.             SWSetCurrentFrameIndex(shadowSpriteP, 2);
  117.  
  118.                 // set the sprite’s initial location
  119.             SWSetSpriteLocation(shadowSpriteP, itemRect.left, itemRect.top);
  120.             GetDItemRect(aboutDialogP, 5, &itemRect);
  121.             SWSetSpriteLocation(earthSpriteP, itemRect.left, itemRect.top);
  122.  
  123.             ((WindowPeek)aboutDialogP)->refCon = (long)spriteWorldP;
  124.  
  125.             ShowWindow(aboutDialogP);
  126.             SetPort(aboutDialogP);
  127.             DrawDialog(aboutDialogP);
  128.             EraseRect(&itemRect);
  129.             OutlineDefaultButton(aboutDialogP, ok);
  130.  
  131.             SWLockSpriteWorld(spriteWorldP);
  132.  
  133.             CopyBits(&aboutDialogP->portBits,
  134.                         (BitMap*)spriteWorldP->backFrameP->framePix,
  135.                         &aboutDialogP->portRect,
  136.                         &aboutDialogP->portRect,
  137.                         srcCopy, NULL);
  138.  
  139.  
  140.             spriteWorldDialogFilter = NewModalFilterProc(AboutDialogFilter);
  141.  
  142.             do
  143.             {
  144.                 ModalDialog(spriteWorldDialogFilter, &itemHit);
  145.             } while (itemHit != ok);
  146.  
  147.             DisposeRoutineDescriptor(spriteWorldDialogFilter);
  148.             SWUnlockSpriteWorld(spriteWorldP);
  149.         }
  150.  
  151.         DisposeDialog(aboutDialogP);
  152.         SWDisposeSpriteWorld(&spriteWorldP);
  153.  
  154.         SetPort(savePort);
  155.  
  156.         if (err != noErr)
  157.         {
  158.             ErrorAlert(err, kUnknownErrorStringIndex);
  159.         }
  160.     }
  161.     else
  162.     {
  163.         ErrorAlert(ResError(), kCantFindResourceStringIndex);
  164.     }
  165. }
  166.  
  167.  
  168. ///--------------------------------------------------------------------------------------
  169. // EarthMoveProc
  170. ///--------------------------------------------------------------------------------------
  171.  
  172. SW_FUNC void EarthMoveProc(
  173.     SpritePtr srcSpriteP)
  174. {
  175.         // gravity
  176.     if (srcSpriteP->vertMoveDelta < (short)kMaxSpeed)
  177.         srcSpriteP->vertMoveDelta++;
  178.  
  179.     SWOffsetSprite( srcSpriteP, 0, srcSpriteP->vertMoveDelta );
  180.         // bounce
  181.     if (srcSpriteP->destFrameRect.bottom > srcSpriteP->moveBoundsRect.bottom)
  182.     {
  183.         srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  184.         
  185.         SWMoveSprite( srcSpriteP, srcSpriteP->destFrameRect.left, 
  186.             srcSpriteP->moveBoundsRect.bottom -
  187.             (srcSpriteP->curFrameP->frameRect.bottom - 
  188.             srcSpriteP->curFrameP->frameRect.top));
  189.     }
  190. }
  191.  
  192.  
  193. ///--------------------------------------------------------------------------------------
  194. // ShadowFrameProc
  195. ///--------------------------------------------------------------------------------------
  196.  
  197. SW_FUNC void ShadowFrameProc(
  198.     SpritePtr srcSpriteP,
  199.     FramePtr curFrameP,
  200.     long* curFrameIndex)
  201. {
  202.     #pragma        unused(curFrameP)
  203.  
  204.     SpritePtr earthSpriteP = (SpritePtr)srcSpriteP->userData;
  205.     short distanceFromTop = earthSpriteP->destFrameRect.top - srcSpriteP->moveBoundsRect.top;
  206.     short height = earthSpriteP->moveBoundsRect.bottom - earthSpriteP->moveBoundsRect.top;
  207.  
  208.     *curFrameIndex = (long)((distanceFromTop * 5) / height);
  209.  
  210.         // lets not index past the last frame!
  211.         // (even though SpriteWorld will not allow it to happen)
  212.     if (*curFrameIndex > 4L) *curFrameIndex = 4L;
  213. }
  214.  
  215.  
  216. ///--------------------------------------------------------------------------------------
  217. // AboutDialogFilter
  218. ///--------------------------------------------------------------------------------------
  219.  
  220. pascal Boolean AboutDialogFilter(
  221.     DialogPtr aboutDialogP,
  222.     EventRecord *event,
  223.     short *itemHit)
  224. {
  225.     Boolean eventHandled = false;
  226.     SpriteWorldPtr spriteWorldP = (SpriteWorldPtr)((WindowPeek)aboutDialogP)->refCon;
  227.  
  228.     switch (event->what)
  229.     {
  230.         case nullEvent:
  231.         {
  232.                 // run the about box animation
  233.             SWProcessSpriteWorld(spriteWorldP);
  234.             SWAnimateSpriteWorld(spriteWorldP);
  235.  
  236.                 // keep the title animation going!
  237.             SpriteTestIdle(gSpriteTestP);
  238.             break;
  239.         }
  240.  
  241.         case keyDown:
  242.         case autoKey:
  243.         {
  244.             char key = (char)(event->message & charCodeMask);
  245.  
  246.             if (key == kReturnChar || (key == kEnterChar))
  247.             {
  248.                 *itemHit = ok;
  249.                 eventHandled = true;
  250.                 ClickDialogButton(aboutDialogP, ok);
  251.             }
  252.  
  253.             break;
  254.         }
  255.  
  256.         case updateEvt:
  257.         {
  258.             if ((DialogPtr)event->message == aboutDialogP)
  259.             {
  260.                 SetPort(aboutDialogP);
  261.                 BeginUpdate(aboutDialogP);
  262.  
  263.                 SWUpdateSpriteWorld(spriteWorldP, true);
  264.                 
  265.                 EndUpdate(aboutDialogP);
  266.                 eventHandled = true;
  267.             }
  268.             else
  269.             {
  270.                 HandleUpdateEvent((WindowPtr)event->message);
  271.             }
  272.  
  273.             break;
  274.         }
  275.     }
  276.  
  277.     return eventHandled;
  278. }
  279.  
  280. /**********************************  DrawVersionProc  *******/
  281. pascal void DrawVersionProc (DialogPtr theDialog, short itemNo)
  282.  
  283. {
  284.     Rect            itemRect;
  285.     short            itemType;
  286.     Handle            itemHandle;
  287.     unsigned long    versNum;
  288.     short            majorNum;
  289.     short            betaNum;
  290.     char            versDigit1, versDigit2, versDigit3;
  291.     
  292.     
  293.     
  294.     GetDialogItem ( theDialog, itemNo, &itemType, &itemHandle, &itemRect );
  295.     
  296.     versNum = SWGetSpriteWorldVersion();
  297.     
  298.     majorNum = HiWord( versNum );
  299.     majorNum>>=8;
  300.     versDigit1 = majorNum | '0';
  301.     majorNum = (HiWord( versNum ))&0x00FF;
  302.     majorNum >>=4;
  303.     versDigit2 = majorNum | '0';
  304.     majorNum = (HiWord( versNum ))&0x000F;
  305.     versDigit3 = majorNum | '0';
  306.     
  307.     MoveTo( itemRect.left, itemRect.top + 9 );
  308.     TextFont(applFont);
  309.     TextSize(9);
  310.     DrawChar( versDigit1 );
  311.     DrawChar( '.' );
  312.     DrawChar( versDigit2 );
  313.     if ( versDigit3 > '0' )
  314.     {
  315.         DrawChar( '.' );
  316.         DrawChar( versDigit3 );
  317.     }
  318.  
  319.     betaNum = LoWord( versNum );
  320.     if ( betaNum )
  321.     {
  322.         ForeColor( blueColor );
  323.         DrawString( "\p beta" );
  324.         DrawChar( '0'+betaNum );
  325.         ForeColor( blackColor );
  326.     }
  327.     DrawString( "\p by" );
  328.     TextFace( 0 );
  329.     TextFont(systemFont);
  330.     TextSize(12);
  331. }
  332.  
  333.  
  334.